package com.superc;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.superc.lib.helper.KeyValue;
import com.superc.lib.intent.DirectorySelector;
import com.superc.lib.intent.IntentFunction;
import com.superc.lib.ui.KeyValueArrayAdapter;
import com.superc.lib.ui.UIFunction;
import com.superc.model.audio.RecordSetting;
import com.superc.model.intent.SelectionInfo;

public class AudioSetting extends Activity {

	private RecordSetting recordSetting = null;
	private Spinner cboSampleRate = null;
	private Spinner cboChannel = null;
	private Spinner cboEncoding = null;
	private EditText txtBufferSize = null;
	private Context context=null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.audiosetting);
		this.context=this;
		Bundle recordParam = getIntent().getExtras();
		if (recordParam != null) {
			recordSetting = (RecordSetting) recordParam
					.getSerializable(RecordSetting.RecordSettingKey);
			initUI();
		}
		
	}

	@SuppressWarnings("unchecked")
	private void initUI() {
		// Setup ui
		cboSampleRate = UIFunction.createSpinnerFromArray(
				this,
				R.id.cboSampleCode,
				RecordSetting.getAllSampleRate(getResources().getStringArray(
						R.array.arrStrSample)));
		cboChannel = UIFunction.createSpinnerFromArray(this, R.id.cboChannel,
				RecordSetting.getAllChannel());
		cboEncoding = UIFunction.createSpinnerFromArray(this, R.id.cboEncoding,
				RecordSetting.getAllEncoding());
		txtBufferSize = UIFunction.getEditText(this, R.id.txtBufferSize);
		Button cmdNext = UIFunction.getButton(this, R.id.cmdNext);
		// Set init value
		cboSampleRate.setSelection(
				((KeyValueArrayAdapter<String, Integer>) cboSampleRate
						.getAdapter()).findPositionByValue(recordSetting
						.getSampleRate()), true);
		cboChannel.setSelection(
				((KeyValueArrayAdapter<String, Integer>) cboChannel
						.getAdapter()).findPositionByValue(recordSetting
						.getChannel()), true);
		cboEncoding.setSelection(
				((KeyValueArrayAdapter<String, Integer>) cboEncoding
						.getAdapter()).findPositionByValue(recordSetting
						.getEncoding()), true);
		txtBufferSize.setText(new Integer(recordSetting.getBufferSize())
				.toString());
		// Set Event
		cmdNext.setOnClickListener(btnClick);
		UIFunction.getButton(this, R.id.cmdBrowseSavePath).setOnClickListener(
				btnClick);
	}

	private View.OnClickListener btnClick = new View.OnClickListener() {
		@SuppressWarnings("unchecked")
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.cmdNext: {
					if (recordSetting != null) {
						recordSetting
								.setSampleRate(((KeyValue<String, Integer>) cboSampleRate
										.getSelectedItem()).getValue().intValue());
						recordSetting
								.setChannel(((KeyValue<String, Integer>) cboChannel
										.getSelectedItem()).getValue().intValue());
						recordSetting
								.setEncoding(((KeyValue<String, Integer>) cboEncoding
										.getSelectedItem()).getValue().intValue());
						recordSetting.setBufferSize(Integer.parseInt(txtBufferSize
								.getText().toString()));
						Intent resultIntent = new Intent();
						resultIntent.putExtra(RecordSetting.RecordSettingKey,
								recordSetting);
						setResult(RESULT_OK, resultIntent);
					}
					finish();
				}
				case R.id.cmdBrowseSavePath:{
					Intent dirSeletorIntent=IntentFunction.getDirectIntent(context, DirectorySelector.class);
					dirSeletorIntent.putExtra(DirectorySelector.BUNDEL_NAME, new SelectionInfo("/"));
					startActivity(dirSeletorIntent);
				}
			}
		}
	};

}